﻿using ModuleA.Event;
using Prism.Commands;
using Prism.Events;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace ModuleA.ViewModels
{
    class ViewCViewModel : IDialogAware
    {
        private readonly IEventAggregator eventAggregator;

        public string Title { get; set; }

        public event Action<IDialogResult> RequestClose;

        public DelegateCommand CancelCommand { get; set; }//设置取消按钮
        public DelegateCommand SaveCommand { get; set; }//设置确定保存按钮
        public ViewCViewModel(IEventAggregator eventAggregator) {//用来订阅事件
            CancelCommand = new DelegateCommand(Cancel);
            SaveCommand = new DelegateCommand(Save);
            this.eventAggregator = eventAggregator;
        }

        private void Save()
        {
            OnDialogClosed();//Save逻辑先不写，先把窗口关闭了
        }

        private void Cancel()
        {
            eventAggregator.GetEvent<MessageEvent>().Publish("Hellow");//在ViewCModel里面注册刚刚添加的MessageEvent文件,向其发送Hello消息

            RequestClose?.Invoke(new DialogResult(ButtonResult.No));//取消绑定No按钮
        }

        /// <summary>
        /// 是否可以关闭窗口，默认填true
        /// </summary>
        /// <returns></returns>
        public bool CanCloseDialog()
        {
            return true;
        }

        /// <summary>
        /// 窗口关闭事件
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public void OnDialogClosed()
        {
            DialogParameters keys = new DialogParameters();//创建对话框传递参数
            keys.Add("Value", "Hello");
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK,keys));//通过委托设定确定按钮会传递keys的值
        }

        /// <summary>
        /// 窗口打开事件
        /// </summary>
        /// <param name="parameters"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnDialogOpened(IDialogParameters parameters)
        {
            Title = parameters.GetValue<string>("Title");
        }
    }
}
